package database;

import java.util.HashMap;
import java.util.Set;

import file.file;
/*******************************************************************************
 * Software: database_auth.java
 * Version:  1.1
 * Date:     2010_12_30_TIME_00:26:49

 * PURPOSE:
 * handling and config of server side database access settings.
 * 
 * a java class object that holds all the informations necessary
 * (user, password, databaseName, url, port etc.) to connect to a database
 * and also informations on where username + passwords of your program can be found in what table and column...
 * 
 * can be found here: (depending on your setup, search for it)
 * /Volumes/DATA/JAVA/SETUP/ECLIPSE/OSX/32BIT/CARBON/eclipse-jee-galileo-SR2-macosx-carbon/Eclipse_JEE.app/Contents/MacOS/endpoint_xml.config

 * License: / Lizenz: GNU General Public License (http://www.gnu.org/licenses/gpl.html) 	 *  	 * English: 		This program is free software; 		you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation; 		either version 3 of the License, or (at your option) any later version.   		This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; 		without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. 		See the GNU General Public License for more details. 		You should have received a copy of the GNU General Public License along with this program; if not, see <http://www.gnu.org/licenses/>.   		 * Excerpt English: 		 * Our General Public Licenses are designed to make sure that you have the freedom to distribute copies of free software (and charge for them if you wish), 		 * that you receive source code or can get it if you want it, that you can change the software or use pieces of it in new free programs, and that you know you can do these things   		Auf Deutsch: 		Dieses Programm ist freie Software. Sie k�nnen es unter den Bedingungen der GNU General Public License, wie von der Free Software Foundation ver�ffentlicht, 		weitergeben und/oder modifizieren, entweder gem�� Version 3 der Lizenz oder (nach Ihrer Option) jeder sp�teren Version. 		Die Ver�ffentlichung dieses Programms erfolgt in der Hoffnung, da� es Ihnen von Nutzen sein wird, aber OHNE IRGENDEINE GARANTIE, 		sogar ohne die implizite Garantie der MARKTREIFE oder der VERWENDBARKEIT F�R EINEN BESTIMMTEN ZWECK. 		Details finden Sie in der GNU General Public License. 		Sie sollten ein Exemplar der GNU General Public License zusammen mit diesem Programm erhalten haben. 		Falls nicht, siehe <http://www.gnu.org/licenses/>.   		 * Auszug Deutsch: 		 * Wenn wir von freier Software sprechen, so beziehen wir uns auf Freiheit, nicht auf den Preis. 		 * Unsere Allgemeinen �ffentlichen Lizenzen sind darauf angelegt, sicherzustellen, da� Sie die Freiheit haben, 		 * Kopien freier Software zu verbreiten (und daf�r etwas zu berechnen, wenn Sie m�chten), die M�glichkeit, 		 * da� Sie die Software als Quelltext erhalten oder den Quelltext auf Wunsch bekommen, da� Sie die Software 		 * �ndern oder Teile davon in neuen freien Programmen verwenden d�rfen und da� Sie wissen, da� Sie dies alles tun d�rfen.  / Lizenz: GNU General Public License (http://www.gnu.org/licenses/gpl.html)
		 * 
		 * English:
		 This program is free software;
		 you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation;
		 either version 3 of the License, or (at your option) any later version.
		 
		 This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
		 without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
		 See the GNU General Public License for more details.
		 You should have received a copy of the GNU General Public License along with this program; if not, see <http://www.gnu.org/licenses/>.
		 
		 * Excerpt English:
		 * Our General Public Licenses are designed to make sure that you have the freedom to distribute copies of free software (and charge for them if you wish),
		 * that you receive source code or can get it if you want it, that you can change the software or use pieces of it in new free programs, and that you know you can do these things
		 
		 Auf Deutsch:
		 Dieses Programm ist freie Software. Sie k�nnen es unter den Bedingungen der GNU General Public License, wie von der Free Software Foundation ver�ffentlicht,
		 weitergeben und/oder modifizieren, entweder gem�� Version 3 der Lizenz oder (nach Ihrer Option) jeder sp�teren Version.
		 Die Ver�ffentlichung dieses Programms erfolgt in der Hoffnung, da� es Ihnen von Nutzen sein wird, aber OHNE IRGENDEINE GARANTIE,
		 sogar ohne die implizite Garantie der MARKTREIFE oder der VERWENDBARKEIT F�R EINEN BESTIMMTEN ZWECK.
		 Details finden Sie in der GNU General Public License.
		 Sie sollten ein Exemplar der GNU General Public License zusammen mit diesem Programm erhalten haben.
		 Falls nicht, siehe <http://www.gnu.org/licenses/>.
		 
		 * Auszug Deutsch:
		 * Wenn wir von freier Software sprechen, so beziehen wir uns auf Freiheit, nicht auf den Preis.
		 * Unsere Allgemeinen �ffentlichen Lizenzen sind darauf angelegt, sicherzustellen, da� Sie die Freiheit haben,
		 * Kopien freier Software zu verbreiten (und daf�r etwas zu berechnen, wenn Sie m�chten), die M�glichkeit,
		 * da� Sie die Software als Quelltext erhalten oder den Quelltext auf Wunsch bekommen, da� Sie die Software
		 * �ndern oder Teile davon in neuen freien Programmen verwenden d�rfen und da� Sie wissen, da� Sie dies alles tun d�rfen.
		 * 

 * FEATURES:
	 * 
 * USAGE?
	 * 
 * CHANGELOG / CREDITS:
		* WHO?							| WHAT?															| PRECIOUS TIME SPEND?	|�WHY?
 * 
 *******************************************************************************/
public class database_auth
{
	/** properties **/
	/** url to the host e.g. localhost or ip adress or domain name without http:// **/
	public String host = "";

	/** port where database is reachable **/
	public String port = "";

	/** the name of the database when it was created **/
	public String DatabaseName = "";
	
	/** table that will be interacted with **/
	public String TableName = "";
	
	/** a user that has access rights to the database
	 * if nothing is passed here, it will be read from endpoint_xml.config **/
	public String user = "";
	
	/** name of the column where the username is stored
	 * this has nothing to do with the mysql-username and password used to access the database,
	 * it defines where in what table and column are the auth data associated with the users of your program!
	* if nothing is passed here, it will be read from endpoint_xml.config **/
	public String usernameColumn = "";
	
	/** name of the column where the username is stored
	 * this has nothing to do with the mysql-username and password used to access the database,
	 * it defines where in what table and column are the auth data associated with the users of your program!
	* if nothing is passed here, it will be read from endpoint_xml.config **/
	public String passwordColumn = "";
	
	/** name of the table of the database where a list of all users is stored
	 * this has nothing to do with the mysql-username and password used to access the database,
	 * it defines where in what table and column are the auth data associated with the users of your program!
	 * if nothing is passed here, it will be read from endpoint_xml.config **/
	public String authTable = "";
	
	/** password for user to access the database
	 * if nothing is passed here, it will be read from endpoint_xml.config
	 * **/
	public String password = "";
	
	/** wether to use password encryption or not
	 * possible values:
	 * yes, no, pgp (not implemented yet)
	 * **/
	public String passwordEncryption = "no";
	
    /** what kind of database is used possible values: mysql **/
	public String type = "mysql";
	
	public file FILE = new file();

    /** stores infos from the file **/
    HashMap<String, String> endpoint_xml = new HashMap<String, String>();
    
    /** read all parameters from config file endpoint_xml.config
     * if the file does not exist, create it with default layout: **/
    public StringBuffer endpoint_xml_defaultLayout()
    {
    	StringBuffer stringbuffer = new StringBuffer();
   		stringbuffer.append("######## MYSQL CONFIG ########\n");
   		stringbuffer.append("host=localhost\n");
   		stringbuffer.append("DatabaseName=\n");
   		stringbuffer.append("type=mysql\n");
   		stringbuffer.append("port=3306\n");
   		stringbuffer.append("user=\n");
   		stringbuffer.append("password=\n");
   		stringbuffer.append("passwordEncryption=no\n");
   		stringbuffer.append("usernameColumn=username\n");
   		stringbuffer.append("passwordColumn=password\n");
   		stringbuffer.append("authTable=users\n");
   		
   		return stringbuffer;
    }

    public database_auth()
    {
    	// if the config file does not exist, create it with default layout:
    	if(!FILE.exists("endpoint_xml.config"))
    	{
    		FILE.createNewTextFile("endpoint_xml.config", endpoint_xml_defaultLayout());
    	}

    	HashMap<String, String> endpoint_xml = FILE.readConfigFile("endpoint_xml.config", "=");
    	
    	this.host = endpoint_xml.get("host");
    	this.DatabaseName = endpoint_xml.get("DatabaseName");
    	
    	this.type = endpoint_xml.get("type");
    	this.port = endpoint_xml.get("port");
    	
    	this.user = endpoint_xml.get("user");
    	this.password = endpoint_xml.get("password");
    	this.passwordEncryption = endpoint_xml.get("passwordEncryption");
    	this.authTable = endpoint_xml.get("authTable");
    	this.usernameColumn = endpoint_xml.get("usernameColumn");
    	this.passwordColumn = endpoint_xml.get("passwordColumn");
    }

    /** read all parameters from the parameters passed. **/
    public database_auth(String host, String port, String DatabaseName, String type, String TableName, String user, String password)
	{
		this.host = host;
		this.DatabaseName = DatabaseName;

		this.type = type;
		this.port = port;

		this.user = user;
		this.password = password;
		this.passwordEncryption = passwordEncryption;
		this.authTable = endpoint_xml.get("authTable");
		this.usernameColumn = endpoint_xml.get("usernameColumn");
		this.passwordColumn = endpoint_xml.get("passwordColumn");
	}
}
